/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package peggame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;
import java.util.TreeSet;

/**
 *
 * @author zach
 */
public class Driver {
    private Stack<Instance> stack=new Stack<Instance>();
    private Instance current;
    private HashMap<Integer,Instance> pervious =new HashMap<Integer,Instance>();
    public String out="";
    public Driver(pegmanager in,MoveChecker mc){
        stack.push(new Instance(new Move(in.startx,in.starty,in.startx,in.starty),null,in));
        System.out.println(stack.peek().getBoard().NumPegs());
        current=stack.peek();
        while(current.getLastmove().lastx!=in.endx&&current.getLastmove().lasty!=in.endy){
            current=stack.pop();
            
            current.openings();
            ArrayList<Integer> openXs=current.openX;
            ArrayList<Integer> openYs=current.openY;
            TreeSet<Move> moves=mc.showMeYourMoves(openXs.get(0), openYs.get(0), current.getBoard());

            for(int i=0;i<openXs.size();i++){
                moves.addAll(mc.showMeYourMoves(openXs.get(i), openYs.get(i), current.getBoard()));
            }

            for(Move i:moves){
                
                Instance zig=new Instance(i,current,current.getBoard());
                if(!pervious.containsKey(zig.hashCode())){
                    pervious.put(zig.hashCode(),zig);
                    stack.push(zig);
                }
            }
        }
        
        out=stack.peek().toString();
    }

    public String getOut() {
        return out;
    }

}
